<?php
/**
 * Created by PhpStorm.
 * User: 14775
 * Date: 2017/9/7
 * Time: 15:10
 */

namespace Admin\Controller;


use Common\Controller\AdminbaseController;
use Common\Model\MerchantModel;
use Common\Model\PromotionCouponBannersModel;
use Common\Model\PromotionCouponModel;

class PromotionCouponController extends AdminbaseController
{
    protected $coupon_model;
    protected $merchant_model;

    public function _initialize()
    {
        parent::_initialize();
        $this->coupon_model = new PromotionCouponModel();
        $this->merchant_model = new MerchantModel();
    }


    public function index()
    {


        /**搜索条件**/
        $coupon_name = I('request.coupon_name');

        $where = array();
        if ($coupon_name) {
            $where['coupon_name'] = array('like', "%$coupon_name%");
        }

        $count = $this->coupon_model->where($where)->count();
        $page = $this->page($count, 10);
        $list = $this->coupon_model
            ->where($where)
            ->order("id DESC")
            ->limit($page->firstRow, $page->listRows)
            ->select();

        foreach ($list as & $item) {
            $merchant = $this->merchant_model->where(array('id' => $item['merchant_id']))->find();
            $item['merchant_name'] = $merchant['name'];
        }

        $this->assign("page", $page->show('Admin'));
        $this->assign('list', $list);
        $this->display();

    }

    public function add()
    {
        $merchants = $this->merchant_model->where(array('use_flg' => 1))->select();
        $this->assign('merchants', $merchants);
        $this->display();
    }

    public function edit()
    {
        $coupon_banner_model = new PromotionCouponBannersModel();
        $id = I('get.id', 0, 'intval');
        $coupon = $this->coupon_model->where(array('id'=>$id))->find();
        $banners = $coupon_banner_model->where(array('coupon_id'=>$id))->select();

        $merchants = $this->merchant_model->where(array('use_flg' => 1))->select();
        $this->assign('merchants', $merchants);
        $this->assign('coupon', $coupon);
        $this->assign('banners', $banners);
        $this->display();
    }


    public function edit_post()
    {
        $coupon_banner_model = new PromotionCouponBannersModel();
        $params = I('');


        $dataArray = array(
            'id'=> $params['id'],
            'coupon_name' => $params['coupon_name'],
            'title' => $params['title'],
            'title2' => $params['title2'],
            'merchant_id' => $params['merchant_id'],
            'start_date' => $params['start_date'],
            'end_date' => $params['end_date'],
            'use_limit' => $params['use_limit'],
            'discount_limit' => $params['discount_limit'],
            'total_num' => $params['total_num'],
            'promotion_img' => $params['promotion_img'],
            'card_img' => $params['card_img'],
            'sort' => $params['sort'],
            'use_flg' => $params['use_flg'],
            'description' => $params['description'],
            //'use_type' => 1,
            //'coupon_type' => 2,
            //'code_length' => 16,
            //'amount' => 0,
            'exintegral' => $params['exintegral']
        );

        $this->coupon_model->save($dataArray);
        $coupon_banner_model->where(array('coupon_id'=>$dataArray['id']))->delete();
        if ($dataArray['id'] && $params['photos_url']) {
            foreach ($params['photos_url'] as $url) {
                $banner_data = array(
                    'coupon_id' => $dataArray['id'],
                    'banner_img' => $url,
                    'use_flg' => 1
                );
                $coupon_banner_model->add($banner_data);
            }
            $this->success("编缉成功！");
        } else {
            $this->error("编缉失败！".$this->coupon_model->getError());
        }
    }


    public function add_post()
    {
        $coupon_banner_model = new PromotionCouponBannersModel();
        $params = I('');


        $dataArray = array(
            'coupon_name' => $params['coupon_name'],
            'title' => $params['title'],
            'title2' => $params['title2'],
            'merchant_id' => $params['merchant_id'],
            'start_date' => $params['start_date'],
            'end_date' => $params['end_date'],
            'use_limit' => $params['use_limit'],
            'discount_limit' => $params['discount_limit'],
            'total_num' => $params['total_num'],
            'promotion_img' => $params['promotion_img'],
            'card_img' => $params['card_img'],
            'sort' => $params['sort'],
            'use_flg' => $params['use_flg'],
            'description' => $params['description'],
            'use_type' => 1,
            'coupon_type' => 2,
            'code_length' => 16,
            'amount' => 0,
            'exintegral' => $params['exintegral']
        );

        $couponId = $this->coupon_model->add($dataArray);
        if ($couponId && $params['photos_url']) {
            foreach ($params['photos_url'] as $url) {
                $banner_data = array(
                    'coupon_id' => $couponId,
                    'banner_img' => $url,
                    'use_flg' => 1
                );
                $coupon_banner_model->add($banner_data);
            }
            $this->success("添加成功！");
        } else {
            $this->error("添加失败！");
        }
    }

    // 优惠券删除
    public function delete()
    {
        $id = I('get.id', 0, 'intval');
        $this->coupon_model->delete($id);
        $this->success("删除成功！");
    }
}